cap log close
log using "${logpath}clean_claims.log", replace

/*******************************************************************************
clean_claims.do

This code cleans the NBER claims data
*******************************************************************************/

********************************************************************
**** Collect Ambulance Taxi Claims

foreach YEAR of numlist 1999/2019 {
	use if inlist(hcpcs_cd, "A0428", "A0425", "A0426", "A0427", "A0429", "A0433") using "${rawdatapath}car/`YEAR'/carl`YEAR'.dta", clear

	*A0425 = Mileage
	*A0428 = Base ride 

	keep if inlist(hcpcs_cd, "A0425", "A0428")

	* drop whitespace in mdfr codes
	replace mdfr_cd1 = trim(mdfr_cd1)

	* Keep all mileage and any base rides that are to/from dialysis

	keep if (hcpcs_cd == "A0425") | inlist(mdfr_cd1, "EG", "NG", "RG", "EJ", "NJ", "RJ") | inlist(mdfr_cd1, "GE", "GN", "GR", "JE", "JN", "JR")


	* Only keep mileage if it matches a base ride 
	gen base = 1 if hcpcs == "A0428"
	replace base = 0 if mi(base)

	gen mileage = 1 if hcpcs == "A0425"
	replace mileage = 0 if mi(mileage)

	bysort clm_id: egen hasbase = max(base)
	keep if hasbase == 1 

	tempfile claims`YEAR'

	save `claims`YEAR'', replace
}

******************************************************
** Make a big panel
* Start with 2007 cuz of NPIs 
clear 
foreach YEAR of numlist 2007/2019 {
	append using `claims`YEAR''
}

** Prep for panel
gen month = mofd(thru_dt)
gen ride = 1 if hcpcs_cd == "A0428"
rename prf_npi npi 

count 
save "${cleandatapath}ambulancetaxis_claims_0719.dta", replace 

**********************************************************************
* Make Firm Panel

collapse (sum) ride (sum) linepmt (first) provzip, by(npi month)

drop if mi(npi)
destring npi, replace force
drop if mi(npi)

* Panelize and fill 
tsset npi month
tsfill, full 
	* This is also filling backwards

** Fill 0 payments and rides 
replace ride = 0 if mi(ride)
replace linepmt = 0 if mi(linepmt)

* Fix zip type 
gen zip = substr(provzip, 1, 5)
destring zip, replace force

** Carry zips forward/backwards in panel
sort npi month
bysort npi: carryforward zip, replace

gen negmonth = -1*month
sort npi negmonth 
bysort npi: carryforward zip, replace
drop negmonth 
sort npi month 

** Merge firm enforcement 
merge m:1 npi using "${cleandatapath}Indicted_NPIs.dta", keep(master match) nogen
replace indictednpi = 0 if mi(indictednpi)

**** Merge location-level enforcement

merge m:1 zip using "${cleandatapath}zip_doj_crosswalk.dta", keep(master match) nogen 
merge m:1 district using "${cleandatapath}DOJ_data_district.dta", nogen 

rename (civ_backupdate crim_backupdate) (district_treat_civil district_treat_criminal)


****************************************************************************
** Add PA dates

gen padate = .
replace padate = mofd(mdy(12, 1, 2014)) if inlist(state, "SC", "PA", "NJ")
replace padate = mofd(mdy(1, 1, 2016)) if inlist(state,"NC", "VA", "DC", "MD", "WV", "DE") 


****************************************************************************
**** Save and export
save "${cleandatapath}ambulancetaxis_firmpanel_enforcement.dta", replace

log close